package com.simple.datax.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.simple.datax.bo.IDataxDictionaryBO;
import com.simple.datax.bo.IDataxDictionaryCatagoryBO;
import com.simple.datax.vo.DataxDictionaryIdVO;
import com.simple.datax.vo.DataxDictionaryVO;
import com.simple.util.object.JFValidationBean;
import com.simple.util.object.PageInfoBean;

@Controller 
public class TransPropertyController extends AbstractDataxBaseController {
	
	private final static String JSP_PATH="_TransProperty";
	@Autowired
	private IDataxDictionaryBO  dataxDictionaryBO;
	
	@Autowired
	private IDataxDictionaryCatagoryBO  dataxDictionaryCatagoryBO;

	@RequestMapping("/transProperty")  
    public String init(@RequestParam(value="pageNo",required=false,defaultValue="1") Integer pageNo,
    				   @RequestParam(value="fcCodeSelect",required=false) String fcCodeSelect,
    				   @RequestParam(value="fcNameSelect",required=false) String fcNameSelect,
    				   @RequestParam(value="fiCatagorySelect",required=false,defaultValue="1") String fiCatagorySelect,
    				   HttpServletRequest request, HttpServletResponse response)  {
		
        final PageInfoBean pageInfoBean = new PageInfoBean(pageNo);
		final List<DataxDictionaryVO> dataxDictionaryVOList = dataxDictionaryBO.findDataxDictionaryVOByPage(pageInfoBean, fcCodeSelect, fcNameSelect, fiCatagorySelect);
		request.setAttribute("dataxDictionaryVOList", dataxDictionaryVOList);
		 
		final String pageInfo = super.generatePageInfo(request,response,pageInfoBean, "goPage");
	    request.setAttribute("pageInfo", pageInfo);  
	    
	    request.setAttribute("pageNo", pageNo); 
	    request.setAttribute("fcCodeSelect", fcCodeSelect);  
	    request.setAttribute("fcNameSelect", fcNameSelect);  
	    request.setAttribute("fiCatagorySelect", fiCatagorySelect); 
	    request.setAttribute("TRANS_PROPERTY_FICATAGORY_MAP", dataxDictionaryCatagoryBO.getCatagory());

        return getJspPath("trans_property");
    }
	
	@RequestMapping("/transPropertyAdd")  
    public String addInit(@RequestParam(value="pageNo",required=false,defaultValue="1") Integer pageNo,
					      @RequestParam(value="fcCodeSelect",required=false) String fcCodeSelect,
	    				  @RequestParam(value="fcNameSelect",required=false) String fcNameSelect,
	    				  @RequestParam(value="fiCatagorySelect",required=false) String fiCatagorySelect,
			   			  HttpServletRequest request, HttpServletResponse response)  {
		 
	    request.setAttribute("pageNo", pageNo);  
	    request.setAttribute("fcCodeSelect", fcCodeSelect);  
	    request.setAttribute("fcNameSelect", fcNameSelect);  
	    request.setAttribute("fiCatagorySelect", fiCatagorySelect);
	    request.setAttribute("TRANS_PROPERTY_FICATAGORY_MAP", dataxDictionaryCatagoryBO.getCatagory());
        return getJspPath("trans_property_add");
    }
	
	@RequestMapping(value="/transPropertyAddSubmit",method=RequestMethod.POST)  
    public String addSubmit(@RequestParam(value="pageNo",required=false,defaultValue="1") Integer pageNo,
    				  @RequestParam(value="fcCodeSelect",required=false) String fcCodeSelect,
					  @RequestParam(value="fcNameSelect",required=false) String fcNameSelect,
					  @RequestParam(value="fiCatagorySelect",required=false) String fiCatagorySelect,
    				  @ModelAttribute("dataxDictionaryVO") DataxDictionaryVO dataxDictionaryVO,
    				  HttpServletRequest request, HttpServletResponse response)  {
		
		final List<JFValidationBean> errorList = getErrorList();

		if(dataxDictionaryBO.findById(dataxDictionaryVO.getId())!=null){
			errorList.add(new JFValidationBean("message.transproperty.id.fcCode.is.exist"));
		}

		if(errorList.isEmpty()){
			dataxDictionaryBO.add(dataxDictionaryVO); 
		}
		
		final String strPath;
		if(errorList.isEmpty()){
			strPath=init(pageNo, fcCodeSelect, fcNameSelect, fiCatagorySelect, request, response);
		}else{
			strPath=addInit(pageNo, fcCodeSelect, fcNameSelect, fiCatagorySelect, request, response);
		}
	
		return strPath;
    }
	
	@RequestMapping("/transPropertyEdit")  
    public String editInit(@RequestParam(value="pageNo",required=false,defaultValue="1") Integer pageNo,
				    	   @RequestParam(value="fcCodeSelect",required=false) String fcCodeSelect,
				    	   @RequestParam(value="fcNameSelect",required=false) String fcNameSelect,
				    	   @RequestParam(value="fiCatagorySelect",required=false) String fiCatagorySelect,
				    	   @ModelAttribute("dataxDictionaryIdVO") DataxDictionaryIdVO dataxDictionaryIdVO,
    					   HttpServletRequest request, HttpServletResponse response)  {
		
		final DataxDictionaryVO dataxDictionaryVO = dataxDictionaryBO.findById(dataxDictionaryIdVO);
		request.setAttribute("dataxDictionaryVO", dataxDictionaryVO);  
		
		request.setAttribute("pageNo", pageNo);  
	    request.setAttribute("fcCodeSelect", fcCodeSelect);  
	    request.setAttribute("fcNameSelect", fcNameSelect);  
	    request.setAttribute("fiCatagorySelect", fiCatagorySelect);
	    request.setAttribute("TRANS_PROPERTY_FICATAGORY_MAP", dataxDictionaryCatagoryBO.getCatagory());
        return getJspPath("trans_property_edit");
    }
	
	@RequestMapping("/transPropertyEditSubmit")  
    public String editSubmit(@RequestParam(value="pageNo",required=false,defaultValue="1") Integer pageNo,
    				   @RequestParam(value="fcCodeSelect",required=false) String fcCodeSelect,
					   @RequestParam(value="fcNameSelect",required=false) String fcNameSelect,
					   @RequestParam(value="fiCatagorySelect",required=false) String fiCatagorySelect,
					   @ModelAttribute("dataxDictionaryVO") DataxDictionaryVO dataxDictionaryVO,
    				   HttpServletRequest request, HttpServletResponse response)  {
		
		dataxDictionaryBO.edit(dataxDictionaryVO); 
		request.setAttribute("pageNo", pageNo); 
	    return init(pageNo, fcCodeSelect, fcNameSelect, fiCatagorySelect, request, response);
    }

	@Override
	protected String getBaseJspPath() {
		// TODO Auto-generated method stub
		return JSP_PATH;
	}


}
